package com.example.administrator.yuejian_2.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.bean.MessageBean;
import com.example.administrator.yuejian_2.bean.TrainDetailBean;
import com.example.administrator.yuejian_2.interservice.Const;
import com.example.administrator.yuejian_2.interservice.TrainService;
import com.example.administrator.yuejian_2.interservice.UserDetailService;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class UserMessageDetailActivity extends Activity {
    private TextView system_name,message;
    private ImageView system_pic;
    private int user_id;
    private int message_id;
    private Context context;
    private ImageButton back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_message_detail);
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo",
                Context.MODE_PRIVATE);
        user_id = sharedPreferences.getInt("user_id", 0);
        Intent intent =getIntent();
        message_id = intent.getIntExtra("message_id", -1);
        system_name = findViewById(R.id.system_name);
        system_pic = findViewById(R.id.system_pic);
        message = findViewById(R.id.message);
        back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        getdetail();
    }
    public void getdetail(){
        context = UserMessageDetailActivity.this;
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        UserDetailService service = retrofit.create(UserDetailService.class);
        Call<MessageBean> call = service.messagedetail(message_id);
        call.enqueue(new Callback<MessageBean>() {
            @Override
            public void onResponse(Call<MessageBean> call, Response<MessageBean> response) {
                if (response.body()!= null){
                    String tpic = response.body().getSystem_pic();
                    if (!tpic.equals(""))
                        Picasso.with(context)
                                .load("http://10.0.2.2/YueJian16/Public/clubphoto/"+tpic)
                                .into(system_pic);
                    system_name.setText(response.body().getSystem_name());
                    message.setText(response.body().getMessage());
                }


            }

            @Override
            public void onFailure(Call<MessageBean> call, Throwable t) {

            }
        });

    }
}
